package org.nf.trading.service.app.message.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.dao.MessageMapper;
import org.nf.trading.dao.UserMapper;
import org.nf.trading.dto.ChatMessageDTO;
import org.nf.trading.dto.CommentMessageDTO;
import org.nf.trading.dto.MessageDTO;
import org.nf.trading.entity.Message;
import org.nf.trading.entity.User;
import org.nf.trading.service.app.message.MessageService;
import org.nf.trading.service.app.message.exception.MessageDataException;
import org.nf.trading.service.app.message.websocket.ChatNotifyWebSocketServerHandler;
import org.nf.trading.service.app.message.websocket.NotifyWebSocketServerHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class MessageServiceImpl implements MessageService {
    private final MessageMapper messageMapper;
    private final UserMapper userMapper;

    @Autowired
    private NotifyWebSocketServerHandler handler;

    @Autowired
    private ChatNotifyWebSocketServerHandler chatNotifyWebSocketServerHandler;

    @Override
    public void chatSender(ChatMessageDTO chatMessageDTO) {
        chatNotifyWebSocketServerHandler.chatSend(chatMessageDTO);
    }

    /**
     * 通过消息id查询发送者和接收者
     * @param messageId
     * @return
     */
    @Override
    public CommentMessageDTO messageUser(Integer messageId) {
        if (messageId == null){
            throw new MessageDataException(500,"参数不能为空");
        }
        Message message = messageMapper.selectByPrimaryKey(messageId);
        User senderUser = userMapper.getUserId(message.getSenderId());
        User receiverUser = userMapper.getUserId(message.getReceiverId());
        CommentMessageDTO commentMessageDTO = new CommentMessageDTO();
        commentMessageDTO.setSenderUser(senderUser);
        commentMessageDTO.setReceiverUser(receiverUser);
        return commentMessageDTO;
    }

    @Override
    public List<Message> listMessageByReceiverId(Integer receiverId) {
        if (receiverId == null){
            throw new MessageDataException(500,"参数不能为空");
        }
        return messageMapper.listMessageByReceiverId(receiverId);
    }

    @Override
    public Message getMessageByProductIdIs(Integer productId) {
        if (productId == null){
            throw new MessageDataException(500,"参数不能为空");
        }
        return messageMapper.getMessageByProductIdIs(productId);
    }

    /**
     * 发布
     * @param message
     */
    @Override
    public void publish(MessageDTO message){
        handler.send(message);
    }
    @Override
    public boolean insert(Message message){
        if (message == null){
            throw new MessageDataException(500,"参数为空");
        }
        int insert = messageMapper.insert(message);
        return insert == 1;
    }
    /**
     * 通过用户的id查询属于自己的聊天消息列表
     * @param userId
     * @return
     */
    @Override
    public List<Message> selectByUserId(Integer userId) {
        if (userId == null){
            throw new MessageDataException(500,"userId不能为空");
        }
        return messageMapper.selectByUserId(userId);
    }
}
